package btree;

import java.io.IOException;

import diskmgr.DiskMgrException;
import diskmgr.Page;

import bufmgr.BufMgrException;
import bufmgr.BufferPoolExceededException;
import bufmgr.HashEntryNotFoundException;
import bufmgr.HashOperationException;
import bufmgr.InvalidFrameNumberException;
import bufmgr.PageNotReadException;
import bufmgr.PagePinnedException;
import bufmgr.PageUnpinnedException;
import bufmgr.ReplacerException;

import global.PageId;
import global.RID;
import global.SystemDefs;

public class BTIndexPage extends BTSortedPage {

	public int keyType;

	public BTIndexPage(int key) throws ConstructPageException,
			ReplacerException, HashOperationException, PageUnpinnedException,
			InvalidFrameNumberException, PageNotReadException,
			BufferPoolExceededException, PagePinnedException, BufMgrException,
			IOException, InsertRecException, HashEntryNotFoundException, DiskMgrException {
		super(key);
		setType(NodeType.INDEX);
		keyType = key;
	
//		KeyDataEntry entry=new KeyDataEntry(Integer.MIN_VALUE, id);
//		insertRecord(entry);
	}
	public BTIndexPage(Page page, int keyType) throws IOException {
		  super(page, keyType);
		   this.keyType= keyType;
		  setType(NodeType.INDEX);
		 }
	public BTIndexPage(PageId pid, int keyType) throws ConstructPageException, IOException {
		  super(pid, keyType);
		  this.keyType= keyType  ;
		  setType(NodeType.INDEX);
		 }
public void Init() throws ConstructPageException, BufferPoolExceededException, HashOperationException, ReplacerException, HashEntryNotFoundException, InvalidFrameNumberException, PagePinnedException, PageUnpinnedException, PageNotReadException, BufMgrException, DiskMgrException, IOException{
	BTLeafPage page=new BTLeafPage(keyType);
	PageId id=page.getCurPage();
	setPrevPage(id);
	page.setNextPage(new PageId(7));
}
	public KeyDataEntry getKey(RID rid) throws KeyNotMatchException,
			NodeNotMatchException, ConvertException, IOException {
		return BT.getEntryFromBytes(getpage(), getSlotOffset(rid.slotNo),
				getSlotLength(rid.slotNo), keyType, NodeType.INDEX);
	}

	public KeyDataEntry getFirst(RID rid) throws IOException,
			KeyNotMatchException, NodeNotMatchException, ConvertException {
		RID nextRecord = firstRecord();
		if(nextRecord==null)
			return null;
		rid.slotNo=nextRecord.slotNo;
		rid.pageNo=nextRecord.pageNo;
		KeyDataEntry kDE = getKey(rid);
		return kDE;
	}

	public KeyDataEntry getNext(RID rid) throws IOException,
			KeyNotMatchException, NodeNotMatchException, ConvertException {
		RID nextRecord = nextRecord(rid);
		if(nextRecord==null)
			return null;
		rid.slotNo=nextRecord.slotNo;
		rid.pageNo=nextRecord.pageNo;
		KeyDataEntry kDE = getKey(rid);
		return kDE;
	}

	public PageId getLeftLink() throws IOException, KeyNotMatchException,
			NodeNotMatchException, ConvertException {
//		RID rid = new RID();
//		getFirst(rid);
		return getPrevPage();
	}

	public void setLeftLink(PageId left) throws IOException,
			KeyNotMatchException, NodeNotMatchException, ConvertException {
//		RID rid = new RID();
//		KeyDataEntry k = getFirst(rid);
//		rid.pageNo = left;
	setPrevPage(left);
	}

	public RID insertKey(KeyClass key, PageId pageno) throws InsertRecException {
		KeyDataEntry kDE = new KeyDataEntry(key, new IndexData(pageno));
		RID rid = insertRecord(kDE);
		return rid;
	}
}
